﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace pxt
{
    // holds the spawn points for the level
    public class Level
    {
        // Members
        #region *MEMBERS*
        protected Texture2D mBackground;
        protected List<tSpawnPoint> mSpawns;
        protected int mNumRounds, mCurRound;
        protected List<Round> mRoundList;
        protected List<Team> mTeams;
        // define the rectangle and the owner for the spawns
        protected struct tSpawnPoint
        {
            //public Rectangle mSpawnRect;
            public Vector2 mPos1;
            public Vector2 mPos2;
            public int mTeam; // which team spawns here
        }
        #endregion

        // Functions
        #region *FUNCTIONS*
        public Level()
        {
            mTeams = new List<Team>();
            mCurRound = 1;
        }

        public void Load()
        {
            mBackground = GameGlobals.Content.Load<Texture2D>("levels\\test_level");
            //test.Load();
            //load our fonts

        }

        public void Update(GameTime gt)
        {
            // Update projectiles
            //mAnnouncements.Update(gt);
        }

        public void Draw()
        {
            // draw the level
            //sb.Draw(mBackground, Vector2.Zero, Color.White);
            GameSystems.Graphics.DrawImage(mBackground, Vector2.Zero, Color.White);
            //mAnnouncements.Draw();
        }


        // used for testing - spawn locations hardcoded etc
        public void CreateTestLevel()
        {
            // these will be read in from XML - added at a later date!
            tSpawnPoint spawn1;
            spawn1 = new tSpawnPoint();
            spawn1.mTeam = 1;
            spawn1.mPos1 = new Vector2(10, 125);
            spawn1.mPos2 = new Vector2(110, 125);

            //spawn1.mSpawnRect = new Rectangle(10, 10, 200, 200);

            tSpawnPoint spawn2;
            spawn2 = new tSpawnPoint();
            spawn2.mTeam = 2;
            spawn2.mPos1 = new Vector2(900, 400);
            spawn2.mPos2 = new Vector2(1000, 400);

            //spawn2.mSpawnRect = new Rectangle(400, 400, 200, 200);


            mSpawns = new List<tSpawnPoint>();
            mSpawns.Add(spawn1);
            mSpawns.Add(spawn2);
            // set up the teams

        }

        public void AddTeam(Team tm)
        {
            mTeams.Add(tm);
        }

        public void StartRound()
        {
            // set both teams to active
            foreach (Team t in mTeams)
            {
                // send the spawn position
                //for now the team number is the index
                t.Spawn(mSpawns[t.TeamNumber - 1].mPos1, mSpawns[t.TeamNumber - 1].mPos2);
            }
        }
        public void EndRound(Team winner)
        {
            mCurRound
            mCurRound++;
            if (mCurRound > mNumRounds)
            {
                // Last round has been completed
                // TODO: Handle end of match stuff
            }
        }
        #endregion

        // Properties
        #region *PROPERTIES*
        public List<Team> Teams
        {
            get { return mTeams; }
        }

        public int NumRounds
        {
            get { return mNumRounds; }
            set
            {
                if (value > 0)
                {
                    mNumRounds = value;
                    mRoundList = new List<Round>(mNumRounds);
                }
            }
        }
        #endregion
    }
}
